package com.cyt.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cyt.dao.impl.SPFeiLeiDaoImpl;
import com.cyt.dao.impl.SheJiShiDaoImpl;
import com.cyt.dao.impl.XingWenFengLeiDaoImpl;
import com.cyt.dao.impl.XingWengXiZhiTwoDaoImpl;
import com.cyt.entity.SheJiShi;
import com.cyt.entity.XingWenFengLei;
import com.cyt.entity.XingWengXiZhi;
import com.cyt.util.BaseServlet;
import com.cyt.util.Result;
import com.google.gson.Gson;



public class AdminSheJiShiServlet extends BaseServlet {
	/**
	 * 查设计师所有
	 * @author 喜悦
	 *localhost:8080/shiping/adminSheJiShi?reqName=getShiJiShi
	 */
	public void getShiJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     SheJiShiDaoImpl sjsDaoi=new SheJiShiDaoImpl();//创建设计师的实现类对象
	     List <SheJiShi> sjsLists= sjsDaoi.getSJSsList();//查到的所有设计师
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(sjsLists);//将spLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}

	/**
	 * 查条数
	 * localhost:8080/shiping/adminSheJiShi?reqName=getShiJiShiTiao
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getShiJiShiTiao(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     SheJiShiDaoImpl sjsDaoi=new SheJiShiDaoImpl();//创建设计师的实现类对象
	     List <SheJiShi> sjsLists= sjsDaoi.getSJSsList();//查到的所有设计师
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(sjsLists.size());//将spLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	
	/**
	 * localhost:8080/shiping/adminSheJiShi?reqName=getShiJiShiList
	 * 分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getShiJiShiList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	     
		SheJiShiDaoImpl sjsDaoi=new SheJiShiDaoImpl();
		 int page=Integer.parseInt(request.getParameter("page"));
		 int size=Integer.parseInt(request.getParameter("size"));
		List <SheJiShi> sjsLists= sjsDaoi.getSJS(page, size);
	
	//发送ajax请求,服务器基本上都返回json格式字符串
	
	     response.setCharacterEncoding("UTF-8");//设置响应编码格式
	     PrintWriter out=response.getWriter();//创建out对象
	
	     Gson gson=new Gson();
	     String res =gson.toJson(sjsLists);//将spLists里面的数据转换为json字符串
	     
	     out.print(res);//响应数据
	     
	     out.flush();
	     out.close();
	}
	
	/**
	 * 删除设计师 通过id
	 * localhost:8080/shiping/adminSheJiShi?reqName=shanChuSheJiShi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuSheJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    int sheJiShiId=Integer.parseInt(request.getParameter("categoryId"));//接收前台传来的参数
		SheJiShiDaoImpl shiJiShiDaoi=new SheJiShiDaoImpl();//创建设计师的实现类对象
	    int n =shiJiShiDaoi.shanChuSheJiShi(sheJiShiId);//调用删除的方法,获得数据库受影响行数n
	    response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "删除成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	
	/**
	 * localhost:8080/shiping/adminSheJiShi?reqName=shanChuSheJiShiS
	 * 批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void shanChuSheJiShiS(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
		
		SheJiShiDaoImpl shiJiShiDaoi=new SheJiShiDaoImpl();
	    
		String ids=request.getParameter("ids");
	    int n =shiJiShiDaoi.shanSheJiShiS(ids);
	    
	    if (n>0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));

		}
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * 添加
	 * localhost:8080/shiping/adminSheJiShi?reqName=AddSheJiShi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void AddSheJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    SheJiShiDaoImpl sjsDaoi=new SheJiShiDaoImpl();
		String touxiang=request.getParameter("touxiang");
		String name=request.getParameter("name");
		String zhiwei=request.getParameter("zhiwei");
		
		SheJiShi sjs= new SheJiShi(0, touxiang, name, zhiwei);
        System.out.println(sjs);

		int n= sjsDaoi.addSheJiShi(sjs);
		
	    if (n>0) {
			out.print(Result.toClient(true, "添加成功", ""));
		} else {
			out.print(Result.toClient(false, "添加失败", ""));

		}
		
		
	     
	    out.flush();
	    out.close();
	}
	
	/**
	 * 通过id查数据,前台用于数据回显
	 * localhost:8080/shiping/adminSheJiShi?reqName=chaSheJiShiId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void chaSheJiShiId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    
	    SheJiShiDaoImpl sjsDaoi=new SheJiShiDaoImpl();
		int sjsid=Integer.parseInt(request.getParameter("id"));
		SheJiShi sjs= sjsDaoi.chaSheJiShiId(sjsid);
        
		Gson gson=new Gson();
		String res =gson.toJson(sjs);
	    out.print(res);
	    
	    
	    out.flush();
	    out.close();
	}
	
	
	
	/**
	 * 修改设计师信息
	 * localhost:8080/shiping/adminSheJiShi?reqName=xiuGaiSheJiShi
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiSheJiShi(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
	    PrintWriter out=response.getWriter();
	    SheJiShiDaoImpl sjsDaoi=new SheJiShiDaoImpl();
	    
	    
		int id=0;
		if(request.getParameter("id")!=null && !request.getParameter("id").equals("")) {
			id=Integer.parseInt(request.getParameter("id"));
		}
		String touxiang=request.getParameter("touxiang");
		String name=request.getParameter("name");
		String zhiwei=request.getParameter("zhiwei");
		SheJiShi sjs= new SheJiShi(id, touxiang, name, zhiwei);
		int n =sjsDaoi.xiuGaiSheJiShi(sjs);
	    
	    if (n>0) {//删除成功
			out.print(Result.toClient(true, "修改成功", ""));
		} else {//删除失败
			out.print(Result.toClient(false, "修改失败", ""));

		}
	    out.flush();
	    out.close();
	}
}
